import 'package:flutter/material.dart';
import 'package:flutter_utils/flutter_utils.dart';
import 'package:flutter_widget/src/widget/image/web_cache_Image.dart';
import 'package:flutter_widget/src/widget/tap_widget.dart';
import 'package:flutter_widget/src/widget/text/text_format.dart';

import 'button/go_back_button.dart';

typedef OnTapAction = void Function();

/*通用基础导航栏*/
class Appbar extends StatelessWidget implements PreferredSizeWidget {
  final Color backgroundColor; //背景色
  final String? backgroundImage; //背景图，如果设置将替换颜色
  final double? height; //在此设置基础上会加上安全高度，默认高度44+safeArea
  final OnTapAction? onTap; //点击事件
  final bool isEnableEmptyTap; //点击空白回到顶部事件，如果自定义Controller，需设置scrollController
  final ScrollController? scrollController; //如果自定义ScrollController，需提供
  final Widget? appBar; //自定义AppBar, 如果设置这个，无法使用下面其他属性
  final Brightness? brightness; //状态栏底部颜色，light的话字体显示为黑色， dark字体显示为白色
  final String? titleText; //标题文本
  final double titleTextFontSize;
  final Color? titleTextColor; //标题文本颜色
  final FontWeight? titleFontWeight; //标题粗细
  final String? titleFontName; //字体名称（eg:FontTool.SourceHan）
  final double? titleSpacing; //标题左右空白距离
  final Widget? titleWidget; //自定义title, 如果设置这个将替换titleText, 高度56
  final Color? goBackColor; //返回颜色
  final String? goBackIcon; //返回图标
  final VoidCallback? goBackPressed; //返回事件，可重写
  final Widget? leading; //如果想自定义返回键
  final bool displayLeading; //是否需要左侧按键，默认显示
  final bool automaticallyImplyLeading;
  final List<Widget>? actions; //右侧按键s
  final PreferredSizeWidget? bottom; //导航栏下方位置
  final double? elevation; //阴影
  final bool? centerTitle;

  const Appbar({
    Key? key,
    this.backgroundColor = Colors.transparent,
    this.backgroundImage,
    this.brightness,
    this.onTap,
    this.titleText,
    this.titleTextFontSize = 17,
    this.titleTextColor,
    this.titleFontName,
    this.titleFontWeight,
    this.titleSpacing,
    this.titleWidget,
    this.goBackIcon,
    this.goBackColor,
    this.goBackPressed,
    this.leading,
    this.displayLeading = true,
    this.automaticallyImplyLeading = false,
    this.actions,
    this.isEnableEmptyTap = true,
    this.bottom,
    this.elevation,
    this.height,
    this.scrollController,
    this.appBar,
    this.centerTitle,
  }) : super(key: key);

  Color _bgColor() {
    return backgroundImage?.isNotEmpty == true
        ? Colors.transparent
        : backgroundColor;
  }

  PreferredSizeWidget _appbar() {
    return _AppBar(
      height: height,
      onTap: onTap ?? () {},
      isEnableEmptyTap: isEnableEmptyTap,
      backgroundImage: backgroundImage,
      scrollController: scrollController,
      child: (appBar != null)
          ? PreferredSize(
              preferredSize: Size.fromHeight(height ?? 44),
              child: SafeArea(
                child: appBar!,
              ),
            )
          : AppBar(
              titleSpacing: titleSpacing ?? 0,
              elevation: elevation ?? 0,
              // brightness: this.brightness ?? Brightness.light,
              systemOverlayStyle: brightness == Brightness.dark
                  ? StatusBarUtil.lightText //SystemUiOverlayStyle.light //字体白色
                  : StatusBarUtil.darkText,
              // SystemUiOverlayStyle.dark, //字体黑色
              backgroundColor: _bgColor(),
              centerTitle: centerTitle,
              title: titleWidget ??
                  TextFormatWidget(
                    titleText ?? "",
                    fontSize: titleTextFontSize,
                    color: titleTextColor,
                    fontWeight: titleFontWeight ?? FontWeight.w500,
                    fontName: titleFontName,
                  ),
              leading: displayLeading == true
                  ? (leading ??
                      GoBackButton(
                        iconColor: goBackColor ?? titleTextColor,
                        icon: goBackIcon,
                        onPressed: goBackPressed,
                      ))
                  : null,
              automaticallyImplyLeading: automaticallyImplyLeading,
              actions: actions,
              bottom: bottom,
            ),
    );
  }

  @override
  Widget build(BuildContext context) {
    if (height != null && height != 0) {
      return SizedBox(
        height: height! + Screens.topSafeHeight,
        child: _appbar(),
      );
    }
    return _appbar();
  }

  @override
  Size get preferredSize {
    if (height != null && height != 0) {
      return Size.fromHeight(height!);
    }
    return _appbar().preferredSize;
  }
}

/*基本导航栏，目前封装在base_app_bar内*/
class _AppBar extends StatelessWidget implements PreferredSizeWidget {
  final PreferredSizeWidget? child;
  final ScrollController? scrollController;
  final OnTapAction? onTap; //点击事件，默认回到顶部
  final bool isEnableEmptyTap;
  final String? backgroundImage; //背景图
  final double? height;

  const _AppBar({
    Key? key,
    this.child,
    this.scrollController,
    this.onTap,
    this.backgroundImage,
    this.height,
    this.isEnableEmptyTap = true,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Widget? _child = backgroundImage?.isNotEmpty == true
        ? Stack(
            children: [
              Positioned.fill(
                child: backgroundImage!.contains("http") == true
                    ? WebCacheImage(
                        imageUrl: backgroundImage,
                        fit: BoxFit.cover,
                      )
                    : Image.asset(
                        backgroundImage!,
                        fit: BoxFit.cover,
                      ),
              ),
              child!,
            ],
          )
        : child;

    if (isEnableEmptyTap == true) {
      return TapWidget(
          onTap: onTap ??
              () {
                var scroll =
                    scrollController ?? PrimaryScrollController.of(context);
                scroll?.animateTo(
                  0.0,
                  duration: const Duration(milliseconds: 200),
                  curve: Curves.linear,
                );
              },
          child: _child);
    } else {
      return _child!;
    }
  }

  @override
  Size get preferredSize {
    if (height != null) {
      return Size.fromHeight(height!);
    } else {
      return child!.preferredSize;
    }
  }
}
